//
//  UserAccontViewMedol.swift
//  微博
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccontViewMedol: NSObject {
    
    //定义一个模型数据
    var userAccount: UserAccount?
    
    
    //创建构造方法
    override init() {
        //重新模型加载数据
        userAccount = UserAccount.loadAccount()
        super.init()
    }
    //判断当前的属性
    var userBase:Bool  {
        return userAccount?.access_token != nil
    }
    //用户名
    var userName:String? {
        return userAccount?.name
    }
    //用户图片的url
    var iconURL:NSURL? {
        return NSURL(string: userAccount?.avatar_large ?? "")
    }
    //获取token
    var token:String? {
        return userAccount?.access_token
    }
    
    
    func loadAccess_token(code:String,finished:(success:Bool)->()) {
//        let manager = AFHTTPSessionManager()
//        //设置content类型
//        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        //设置响应类型为文本模式,添加一种模式
        let urlString = "oauth2/access_token"
        //设置参数
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":"http://www.baidu.com"]
        
        let manager = NetworkTools.sharedTools
        manager.requestJSONDict(.POST, urlString:urlString , parameters: parameters) { (result, error) -> () in
            if let dict = result  {
                let account = UserAccount(dict: dict)
                //网络请求数据去获得用户的信息
//                print(account)
                self.loadUserAccont(account,finished: finished)
            }else{
                print(error)
            }
            
        }
        
        
        
//        
//        
//        manager.POST(urlString, parameters:parameters , progress: { (progress) -> Void in
//            print(progress)
//            }, success: { (task, result) -> Void in
//                print(result)
//                //返回字典类型的数据
//                if let dict = result as?[String:AnyObject] {
//                    //创建模型数据
//                    let account = UserAccount(dict: dict)
//                    //网络请求数据去获得用户的信息
//                    print(account)
//                    self.loadUserAccont(account,finished: finished)
//                }
//                
//            }) { (task, error) -> Void in
//                print(error)
//        }
    }
    /*
    *  获取用户信息请求
    */
    private func loadUserAccont(account:UserAccount,finished:(success:Bool)->()) {
        
        //urlString
        let urlString = "2/users/show.json"
        //传递的数据
        //判断access_token是否为空
        if let access_token = account.access_token , let uid = account.uid {
            let parameter = ["access_token":access_token,"uid":uid]
            
            let manager1 = NetworkTools.sharedTools
            manager1.requestJSONDict(.GET, urlString: urlString, parameters: parameter, finished: { (result, error) -> () in
                if error != nil {
                    finished(success: false)
                    return
                }
                    
                    let name = result!["name"]as!String
                    let avatar_large = result!["avatar_large"]as!String
                    account.name = name
                    account.avatar_large = avatar_large
                    print(account)
                    account.saveAccount()
                    finished(success: true)
                
            })
        }

    }
    
    
}
